package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.OutboundBean;
import com.project.bean.OutboundInfoBean;
import com.project.bean.OutboundOrderBean;
import com.project.bean.StockBean;
import com.project.mapper.IOutboundInfoMapper;
import com.project.mapper.IOutboundMapper;
import com.project.mapper.IOutboundOrderMapper;
import com.project.mapper.IStockMapper;
import com.project.service.IOutboundOrderService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class OutboundOrderServiceImpl implements IOutboundOrderService {
    @Autowired
    private IOutboundOrderMapper outboundOrderMapper;
    @Autowired
    private IOutboundInfoMapper outboundInfoMapper;
    @Autowired
    private IOutboundMapper outboundMapper;
    @Autowired
    private IStockMapper stockMapper;

    @Override
    public void out(OutboundOrderBean outboundOrderBean) {//出库
        //1、添加销售基本信息
        outboundOrderMapper.insert(outboundOrderBean);
        //2、根据销售员姓名查询销售车信息集合
        QueryWrapper<OutboundBean> qw = new QueryWrapper<>();
        qw.eq("o_userName", outboundOrderBean.getName());
        List<OutboundBean> list = outboundMapper.selectList(qw);
//        3、遍历销售车集合，循环添加销售明细，循环修改库存数量（累减）
        for (int i = 0; i < list.size(); i++) {
            OutboundBean outbound = list.get(i);
            StockBean stock = stockMapper.selectById(outbound.getStockId());//销售车中的商品
            //添加销售车订单明细
            outboundInfoMapper.insert(new OutboundInfoBean(outboundOrderBean.getOutboundOrderId(),
                    outbound.getStockId(), stock.getOutPrice(), outbound.getNum()));
            stock.setNum(stock.getNum() - outbound.getNum());//库存商品减少
            stockMapper.updateById(stock);//更新
            //、根据销售员id 清空销售车
            outboundMapper.deleteById(outbound.getOutboundId());
        }
    }

    //动态分页查询销售信息表
    @Override
    public IPage<OutboundOrderBean> findByItem(Integer pageNO, String name,
                                               LocalDate startDate, LocalDate endDate) {
        QueryWrapper<OutboundOrderBean> qw = new QueryWrapper<>();
        if (name != null && name.length() != 0) {
            qw.like("ob_connectionName", name);
        }
        if (startDate != null) {
            qw.ge("ob_outDate", startDate);
        }
        if (endDate != null) {
            qw.le("ob_outDate", endDate);
        }
        return outboundOrderMapper.selectPage(new Page<>(pageNO, 3), qw);
    }

    @Override
    public OutboundOrderBean findById(Integer id) {
//        按销售订单id、查询销售详情。同时查询销售明细信息，在销售明细信息中，需要查询商品名称和品牌
        OutboundOrderBean outboundOrder=outboundOrderMapper.selectById(id);
        List<OutboundInfoBean>list=outboundInfoMapper.getOutboundInfo(id);
        outboundOrder.setOutboundInfoList(list);
        return outboundOrder;
    }
}
